package com.pandora.util;

import java.util.Iterator;
import java.util.Map;
import java.util.Set;

import org.objectweb.asm.Type;

import net.sf.cglib.beans.BeanGenerator;
import net.sf.cglib.beans.BeanMap;
import net.sf.cglib.core.Signature;
import net.sf.cglib.proxy.Enhancer;
import net.sf.cglib.proxy.InterfaceMaker;

public class Proxy {

	   private  Object object = null;  
	   
	    /** 
	      * 属性map 
	      */  
	    private  BeanMap beanMap = null;  
	  
	    public Proxy() {  
	        super();  
	    }  
	      
	    @SuppressWarnings("unchecked")  
	    public Proxy(Map propertyMap) {  
	      this.object = generateBean(propertyMap);  
	      this.beanMap = BeanMap.create(this.object);  
	    }  
	  
	    /** 
	      * 给bean属性赋值 
	      * @param property 属性名 
	      * @param value 值 
	      */  
	    public void setValue(String property, Object value) {  
	      beanMap.put(property, value);  
	    }  
	  
	    /** 
	      * 通过属性名得到属性值 
	      * @param property 属性名 
	      * @return 值 
	      */  
	    public Object getValue(String property) {  
	      return beanMap.get(property);  
	    }  
	  
	    /** 
	      * 得到该实体bean对象 
	      * @return 
	      */  
	    public Object getObject() {  
	      return this.object; 
	    }  
	  
	    /** 
	     * @param propertyMap 
	     * @return 
	     */  
	    @SuppressWarnings("unchecked")  
	    private Object generateBean(Map<String,Class<?>> propertyMap) {  
	      Enhancer enhancer=new Enhancer();
	      BeanGenerator generator = new BeanGenerator();  
	      Set<String> keySet = propertyMap.keySet();
	      for (Iterator<String> i = keySet.iterator(); i.hasNext();) {
	       String key =  i.next();
	       generator.addProperty(key, propertyMap.get(key));
	      }
	      return generator.create();
	    }  
	      
}
